/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午5:02:32
 * RoleTool.java
 * 
 */
package com.fujitsu.nanjing.mobile.view.velocity.tools;

import com.fujitsu.nanjing.mobile.exception.NotOneException;
import com.fujitsu.nanjing.mobile.model.hibernate.CoreRole;
import com.fujitsu.nanjing.mobile.service.CoreRoleService;

import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.generic.SafeConfig;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;

/**
 * @author qixb.fnst
 * 
 */
@Configurable("roleTool")
@DefaultKey("role")
@Scope
@DependsOn
public class RoleTool extends SafeConfig {
    private CoreRoleService coreRoleService;

    /**
     * @return the coreRoleService
     */
    public CoreRoleService getCoreRoleService() {
        return coreRoleService;
    }

    /**
     * @param coreRoleService
     *            the coreRoleService to set
     */
    public void setCoreRoleService(CoreRoleService coreRoleService) {
        this.coreRoleService = coreRoleService;
    }

    public String getRoleName(int roleId) {
        CoreRole role = null;
        try {
            role = coreRoleService.getRoleByRoleId(roleId);
        } catch (NotOneException e) {
            return "Unknown";
        }
        if (role == null) {
            return "Unknown";
        } else {
            return role.getRoleName();
        }
    }
}
